<template>
	<view class="box" v-if="is==0">
		<view class="" style="position: relative;">
			<image src="https://web.78keji.cn/wximage/newtop.png" mode="widthFix" style="width: 100%;">
			</image>
			<view class="" style="position: absolute;margin-top: -300rpx;width: 100%;padding:0 80rpx;">
				<u-form style="">
					<u-form-item><u-input v-model="yinhang" placeholder="选择立减金类型" type="select" @click='shows=true'
							placeholder-style="corlor:#999999" /></u-form-item>
				</u-form>
			</view>

			<u-select v-model="shows" :list="listx" @confirm="confirm" title="选择发放立减金银行"></u-select>

		</view>
		<view class="d_f j_c_c" style="width: 100%;">

			<u-button type="success" shape="circle"
				style="position: absolute;width:664rpx;height: 97rpx;margin-top: 6rpx;top: 500rpx;" v-if="openid"
				@click="lq">立即领取</u-button>
			<u-button shape="circle" open-type="getPhoneNumber" @getphonenumber="getphonenumber" type="success"
				style="position: absolute;width:664rpx;height: 97rpx;margin-top: 6rpx;top: 500rpx;"
				v-else>立即领取</u-button>
			<image src="https://web.78keji.cn/wximage/newbts.png" mode="widthFix"
				style="width: 94%;position: absolute;top: 650rpx;"></image>
			<view class="" style="height: 100rpx;">

			</view>

		</view>






	</view>
</template>

<script>
	import {
		sendSms,
		checkSms,
		ljjList,
		wxLogins,
		ljjLq,
		getshenhe,
		getPhone
	} from '../../api/index.js'
	import moment from '../../utils/moment.js'
	export default {
		data() {
			return {
				yinhang: '',
				shows: false,
				is: '',
				content: '',
				border: true,
				showx: false,
				listx: [


					{
						value: "1931",
						label: "工商银行微信立减金"
					},

					{
						value: "3008",
						label: "招商银行微信立减金"
					},
					{
						value: "3009",
						label: "农业银行微信立减金"
					},
					{
						value: "3010",
						label: "建设银行微信立减金"
					},
					{
						value: "3011",
						label: "中国银行微信立减金"
					},


				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '',
				shows: false,
				list: [{
					name: '喜马拉雅'
				}, {
					name: '省心会员'
				}],
				current: 0,
				mobile: "",
				show: false,
				code: "",
				getCodeText: '获取验证码',
				getCodeBtnColor: "#ffffff",
				getCodeisWaiting: false,
				disabled: false,
				openid: "", //是否登录
				proType: '',
				xmlydata: [{
					money: 5,
					status: "0",
					starttime: '2024.01.01',
					endtime: "2099.01.01"

				}],
				tldata: [],
				ljjid: "", //立减金id
				idx: "", //省心会员券id


			}
		},
		components: {

		},
		methods: {
			confirm(e) {
				console.log(e);
				this.yinhang = e[0].label
				this.value = e[0].value

			},
			getphonenumber(e) {
				if (this.value == '') {
					uni.showToast({
						title: '请先选择银行',
						icon: "none"
					}); //弹出提示框
				} else {
					if (e.detail.errMsg === "getPhoneNumber:ok") {
						//这里受机型与模拟器影响，errmsg信息各不相同，只能判断用户是否点击了允许按钮来判断，

						getPhone({
							openid: uni.getStorageSync('openidHF'),
							code: e.detail.code
						}).then((res) => {
							console.log(res)
							uni.setStorageSync('openid', res.data.openid)
							uni.setStorageSync('mobile', res.data.mobile)
							this.openid = uni.getStorageSync('openid')
							this.mobile = uni.getStorageSync('mobile')
							this.proType = 2
							ljjList({
								openid: this.openid,
								type: 3,
								proType: this.proType
							}).then((res) => {
								console.log(res)
								this.xmlydata = res.data
								if (this.xmlydata.length > 0) {

									ljjLq({
										openid: this.openid,
										type: 3,
										id: this.xmlydata[0].id,
										account: uni.getStorageSync('openidHF'),
										accountType: 3,
										bankType: this.value

									}).then((res) => {
										uni.showToast({
											title: res.msg,
											icon: "none"
										}); //弹出提示框
										uni.navigateTo({
											url: '/pages/result/result?flag=1'
										})

									})
								} else {
									uni.navigateTo({
										url: '/pages/result/result?flag=0'
									})
								}


							}).catch((err) => {
								uni.showToast({
									title: err.msg,
									icon: "none"
								}); //弹出提示框
							})

						})
						// code...
					} else {
						//如果没有走以上，均为点击了拒绝授权
						console.log('用户拒绝授权')
						// 通常会在这里弹出模态框，引导用户点击允许授权
					}
				}
				//调用自己后台接口获取手机号
			},
			lq() {
				if (this.value == '') {
					uni.showToast({
						title: '请先选择银行',
						icon: "none"
					}); //弹出提示框
				} else {
					this.proType = 2
					ljjList({
						openid: this.openid,
						type: 3,
						proType: this.proType
					}).then((res) => {
						console.log(res)
						this.xmlydata = res.data
						if (this.xmlydata.length > 0) {

							ljjLq({
								openid: this.openid,
								type: 3,
								id: this.xmlydata[0].id,
								account: uni.getStorageSync('openidHF'),
								accountType: 3,
								bankType: this.value

							}).then((res) => {
								uni.showToast({
									title: res.msg,
									icon: "none"
								}); //弹出提示框
								uni.navigateTo({
									url: '/pages/result/result?flag=1'
								})

							})
						} else {
							uni.navigateTo({
								url: '/pages/result/result?flag=0'
							})
						}


					}).catch((err) => {
						uni.showToast({
							title: err.msg,
							icon: "none"
						}); //弹出提示框
					})
				}

			},
			one() {
				uni.login({
					provider: 'weixin', //使用微信登录
					success: function(loginRes) {
						console.log(loginRes.code);
						// uni.setStorageSync('codexs', loginRes.code)
						getPhone({
							openid: uni.getStorageSync('openidHF'),
							code: loginRes.code
						}).then((res) => {
							console.log(res)
						})

					}
				});

			},
			add(item) {
				this.shows = true
				this.ljjid = item


			},
			viplq(item) {
				this.showx = true
				this.idx = item
			},

			lingqus() {

				uni.showLoading({
					title: '领取中，请稍等',
					mask: true
				});
				ljjLq({
					openid: this.openid,
					type: 3,
					id: this.ljjid,
					account: uni.getStorageSync('openidHF'),
					accountType: 3,
					bankType: this.value

				}).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: "none"
					}); //弹出提示框
					this.getdata()
				})
			},
			change(index) {
				if (!uni.getStorageSync('openid')) {
					uni.showToast({
						title: '请先登录',
						icon: "none"
					}); //弹出提示框

				} else {
					this.current = index;
					this.getdata()
				}

			},
			//推出登录
			loginout() {
				uni.clearStorage()
				this.openid = ''
				this.xmlydata = []
				this.tldata == []
			},
			//获取验证码
			getCode() {
				sendSms({
					mobile: this.mobile,

				}).then((res) => {
					this.disabled = true
					this.getCodeText = "发送中..." //发送验证码
					this.getCodeisWaiting = true;
					setTimeout(() => {
						//this.$common.msg('验证码已发送')
						uni.showToast({
							title: '验证码已发送',
							icon: "none"
						}); //弹出提示框
						this.setTimer(); //调用定时器方法
					}, 1000)
				})
			},
			setTimer() {
				let holdTime = 60; //定义变量并赋值
				this.getCodeText = "重新获取(60)"
				//setInterval（）是一个实现定时调用的函数，可按照指定的周期（以毫秒计）来调用函数或计算表达式。
				//setInterval方法会不停地调用函数，直到 clearInterval被调用或窗口被关闭。
				this.Timer = setInterval(() => {
					if (holdTime <= 0) {
						this.disabled = false
						this.getCodeisWaiting = false;
						this.getCodeBtnColor = "#ffffff";
						this.getCodeText = "获取验证码"
						clearInterval(this.Timer); //清除该函数
						return; //返回前面
					}
					this.getCodeText = "重新获取(" + holdTime + ")"
					holdTime--;
				}, 1000)
			},
			getdata() {
				if (this.current == 0) {
					this.proType = 2
					ljjList({
						openid: this.openid,
						type: 3,
						proType: this.proType
					}).then((res) => {
						console.log(res)
						this.xmlydata = res.data
						console.log(this.xmlydata)
						for (var i in this.xmlydata) {
							if (this.xmlydata[i].status == 0) {
								if (Date.now() < this.xmlydata[i].starttime * 1000) {
									this.xmlydata[i].status = '7'
								}
							}

							this.xmlydata[i].starttime = moment(this.xmlydata[i].starttime * 1000).format(
								"YYYY-MM-DD")
							this.xmlydata[i].endtime = moment(this.xmlydata[i].endtime * 1000).format("YYYY-MM-DD")
						}

					}).catch((err) => {
						uni.clearStorage()
						this.mobile = ''
						this.openid = ''
					})
				} else if (this.current == 1) {
					this.proType = 1
					ljjList({
						openid: this.openid,
						type: 3,
						proType: this.proType
					}).then((res) => {
						console.log(res)
						this.tldata = res.data
						for (var i in this.tldata) {
							if (this.tldata[i].status == 0) {
								if (Date.now() < this.tldata[i].starttime * 1000) {
									this.tldata[i].status = '7'
								}
							}

							this.tldata[i].starttime = moment(this.tldata[i].starttime * 1000).format(
								"YYYY-MM-DD")
							this.tldata[i].endtime = moment(this.tldata[i].endtime * 1000).format("YYYY-MM-DD")
						}
					})
				}

			},


			//点击登录
			login() {
				if (this.code == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入验证码',
						duration: 2000
					});
				} else if (this.mobie == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号',
						duration: 2000
					});
				} else {
					checkSms({
						mobile: this.mobile,
						captcha: this.code,
						type: 3
					}).then((res) => {
						this.show = false
						uni.setStorageSync('openid', res.data.openid)
						uni.setStorageSync('mobile', res.data.mobile)
						this.openid = uni.getStorageSync('openid')
						this.mobile = uni.getStorageSync('mobile')
						this.getdata()
					})
				}

			}
		},
		onLoad() {
			getshenhe({
				pid: 2,
				ver: 48
			}).then((res) => {
				console.log(res)
				this.is = res.data.is
			})
			if (uni.getStorageSync('openid')) {
				this.openid = uni.getStorageSync('openid')
				this.getdata()
			}
			if (uni.getStorageSync('mobile')) {
				this.mobile = uni.getStorageSync('mobile')

			}
			//#ifdef MP-WEIXIN
			if (!uni.getStorageSync('openidHF')) {

				uni.login({
					provider: 'weixin', //使用微信登录
					success: function(loginRes) {
						console.log(loginRes.code);
						// uni.setStorageSync('codexs', loginRes.code)
						wxLogins({
							code: loginRes.code,
							type: 3,
							proid: ""
						}).then((resx) => {
							uni.setStorageSync('openidHF', resx.data.openidH5)

							console.log(resx, 1111)
						})

					}
				});
			}

			//#endif


		},
		watch: {

		},
		computed: {

		}
	}
</script>

<style scoped lang='scss'>
	.money {
		font-size: 90rpx;
	}

	.unit {
		margin-top: 70rpx;
	}

	.box {
		background-color: #FFF0E3;
		min-height: 110vh;
		padding: 24rpx 24rpx 0 24rpx;
	}

	.title {

		font-size: 34rpx;

		font-weight: 400;
		color: #1A1A1A;
		margin-top: 50rpx;

	}

	.boss {
		background-color: white;
		width: 662rpx;

		margin: 0 auto;

		text-align: center;
		position: relative;


	}
</style>